package com.qibo.baselib.api;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.hjq.toast.ToastUtils;
import com.qibo.baselib.BuildConfig;
import com.qibo.baselib.utils.MD5Utils;
import com.qibo.baselib.utils.NetWorkUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by 控制器 on 2017/6/27 0027.
 */

public class BaseMainControl {
    public static   Context mContext;

    public BaseMainControl( Context mContext){
        this.mContext = mContext;
    }

    /***
     * 算时间*/
    public static String time(){
        String sa;
        sa = String.valueOf(new Date().getTime()).substring(0,10);//当前时间戳
        return sa;
    }
    /***
     * 公用参数系统版本/设备名称*/
    public static HashMap<String,String> getMap(){
        HashMap<String,String> map = null;
        try {
            map = new HashMap<String, String>();
            map.put("versionCode", BuildConfig.VERSION_CODE+"");
            map.put("versionName", BuildConfig.VERSION_NAME);
            map.put("os_name","android");
            map.put("login_platform","android");
            map.put("user_id",BaseAppConfig.getInstance().getUserId());
            map.put("os_version",android.os.Build.VERSION.RELEASE);
            map.put("device_name",android.os.Build.MODEL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    /**
     * 公共传参
     * @param sinmap
     */
    public static HashMap<String,String> getGinHandle(HashMap<String,String> sinmap ){
        HashMap<String,String> map = new HashMap<String, String>();
        String timestamp = time();//当前时间戳
        String sign = "";
        String str = timestamp.substring(timestamp.length() - 5, timestamp.length());//当前时间戳后五位
        map.putAll(getMap());
        map.put("timestamp", timestamp);
        map.putAll(sinmap);
        sign= MD5Utils.getSignature(map, timestamp, str);
        map.put("sign", sign);
        Log.e("","参数=2222="+"timestamp=="+timestamp+"sign=="+sign);
        return map;
    }
    /**
     * 公开上传图片
     */
    public static void get_publish(String picture, StringCallback callBack){
        if(isNetwork()){
            ToastUtils.show("网络异常，请重试!");
            return;
        }
        HashMap<String,String> map = new HashMap<String, String>();
        map.put("picture",picture);
        map.putAll(BaseMainControl.getGinHandle(map));
    }
    /**监测网络*/
    public static boolean isNetwork(){
        if(false==isNetwork()){
            return false;
        }
        return true;
    }
    /**
     * 返回网络请求验证
     * @return
     */
    public static Boolean returVerify(Context mContext , final String response){
        if(null != response){
            try {
                if("1".equals(BaseAppJson.getCode(response))){
                    return true;
                }else if("0".equals(BaseAppJson.getCode(response))){
                    Toast.makeText(mContext,BaseAppJson.getMsg(response), Toast.LENGTH_SHORT).show();
                    return false;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 发送短信验证码
     */
    public static void get_sms_auth_code(String mobile, StringCallback callBack){
        if(isNetwork()){
            ToastUtils.show("网络异常，请重试!");
            return;
        }
        HashMap<String,String> map = new HashMap<String, String>();
        map.put("mobile",mobile);
        map.putAll(BaseMainControl.getGinHandle(map));
        for (Map.Entry<String,String> entry:map.entrySet())
        {
            Log.e("tag",entry.getKey()+"=="+entry.getValue());
        }
//        Log.e("tag","url=="+BaseAppConfig.get_reate_star);
        //OkHttpUtils.post().url(BaseAppConfig.get_sms_auth_code).params(map).build().execute(callBack);
    }
    /**
     * 显示图片
     */
    public static void get_ShowImg(String img_url, BitmapCallback callBack){
        if(isNetwork()){
            ToastUtils.show("网络异常，请重试!");
            return;
        }
        OkHttpUtils .get().url(img_url) .build().execute(callBack);
    }

    /*** 版本更新*/
    public static void getVersion(StringCallback callBack){
        if(isNetwork()){
            ToastUtils.show("网络异常，请重试!");
            return;
        }
        HashMap<String,String> map = new HashMap<String, String>();
        map.putAll(BaseMainControl.getGinHandle(map));
        for (Map.Entry<String,String> entry:map.entrySet())
        {
            Log.e("tag",entry.getKey()+"=="+entry.getValue());
        }
        //OkHttpUtils.get().url(BaseAppConfig.get_App_version).params(map).build().execute(callBack);
    }
//    /*** 检测App版本更新*/
//    public static void getVersionUpdate(final Activity mActivity, final int isUP){
//        BaseMainControl.getVersion(new StringCallback() {
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                Log.e("tag==","异常=App="+e+"=call="+call);
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//                Log.e("tag==","数据=App="+response);
//                if(null != response){
//                    try {
//                        Gson gson = new Gson();
//                        AppVersion bean = gson.fromJson(response,AppVersion.class);
//                        if("1".equals(bean.getStatus())){
//                            DialogAppUpdate update = new DialogAppUpdate(mActivity);
//                            update.doUpdate(Integer.valueOf(bean.getData().getVersion_code()),bean.getData().getDownload_url(),bean.getData().getVersion_name(),bean.getData().getContent(),isUP);
//                        }else if("0".equals(bean.getStatus())){
//                            ToastUtils.show(bean.getMessage());
//                        }
//                    } catch (JsonSyntaxException e) {
//                        e.printStackTrace();
//                    } catch (NumberFormatException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        });
//    }
    public static void get_areainfo_json( StringCallback callBack){
        if(isNetwork()){
            return;
        }
        HashMap<String,String> map = new HashMap<String, String>();
        map.putAll(BaseMainControl.getGinHandle(map));
//        for (Map.Entry<String,String> entry:map.entrySet())
//        {
//            Log.e("tag",entry.getKey()+"=="+entry.getValue());
//        }
        //OkHttpUtils.post().url(BaseAppConfig.get_areainfo_json).params(map).build().execute(callBack);
    }
}
